#include <iostream>
#include <errno.h>


using namespace std;

#include "udp4.h"

UDP4::UDP4():Socket(AF_INET, SOCK_DGRAM){}

void UDP4::SetPeerAddress(char* address, int port){

  memset(&m_peer_addr, 0, sizeof(struct sockaddr_in));
  m_peer_addr.sin_port = htons(port);
  m_peer_addr.sin_family = AF_INET;
  inet_pton(AF_INET, address, &m_peer_addr.sin_addr);

}

int UDP4::Receive(void* buffer, int length){

  peer_addr_length = sizeof(m_peer_addr);

  int err = recvfrom(m_sockfd, buffer, length, 0, 
		     (sockaddr*)&m_peer_addr, &peer_addr_length);

  if(err == -1){

    cout << strerror(errno) << endl;

  }
  
  return err;


}

int UDP4::Send(const void* buffer, int length){

  int err = sendto(m_sockfd, buffer, length, 0,
		   (const sockaddr*)&m_peer_addr, sizeof(struct sockaddr));

  if(err == -1){

    cout << strerror(errno) << endl;

  }

  return err;

}
